<?php
require_once('../../../../config.php');
require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_connection', 'yui_json', $CFG->wwwroot.'/mod/assignment/type/seniorproject/seniorproject.js'));

global $CFG, $USER;
$navigation = build_navigation(array('', 'Senior Projects'));
print_header('Senior Projects', 'Senior Projects', $navigation);

$output = '<div id="registrar_wrapper">';

if($projects = get_records('assignment', 'assignmenttype', 'seniorproject')){
	$output .= '<table id="registrar_table" border="1" width="100%">';
	$output .= '<tr><th>Title</th><th>Description</th><th>Student</th><th>BOS</th><th>Sign off</th></tr>';
	foreach($projects as $project){
		$owner_sql = "SELECT * FROM {$CFG->prefix}assignment_submissions INNER JOIN {$CFG->prefix}user ON {$CFG->prefix}assignment_submissions.userid={$CFG->prefix}user.id WHERE assignment={$project->id}";
		$owner = get_record_sql($owner_sql);
		$all_signed = 1;
		if($bos = get_records_sql("SELECT name FROM {$CFG->prefix}bos WHERE id={$project->var5} OR id={$project->var4}")){
			$bos_array = array();
			foreach($bos as $b){
				array_push($bos_array, $b->name);
			}
			$bos = implode(', ', $bos_array);
		}else{
			$bos = '';
		}
		$sql = "SELECT * FROM {$CFG->prefix}seniorproject WHERE assignment_id={$project->id} AND NOT is_registrar";
		if($signatures = get_records_sql($sql)){
			foreach($signatures as $signature){
				if(!$signature->signed){
					$all_signed = 0;
				}
			}
			if($all_signed){
				$sql = "SELECT * FROM {$CFG->prefix}seniorproject WHERE assignment_id={$project->id} AND is_registrar";
				if(!$registrar = get_record_sql($sql)){
					$new_registrar = new object();
					$new_registrar->assignment_id = $project->id;
					$new_registrar->user_id = 0;
					$new_registrar->is_registrar = 1;
					if(insert_record('seniorproject', $new_registrar)){
						$registrar = get_record_sql($sql);
					}else{
						echo "An error occurred.";
					}
				}
				if($registrar->signed){
					$checked = array("checked", "has signed off");
				}else{
					$checked = array("", "has not signed off");
				}
				$output .= '<tr>';
				$output .= '<td class="registrar_signed">'.$owner->data1.'</td>';
				$output .= '<td class="registrar_signed">'.$project->description.'</td>';
				$output .= '<td class="registrar_signed">'.$owner->firstname.' '.$owner->lastname.'</td>';
				$output .= '<td class="registrar_unsigned">'.$bos.'</td>';
				$output .= '<td class="registrar_signed">Registrar sign-off: <input type="checkbox" '.$checked[0].' onClick="mark_registrar(0, '.$project->id.');" /><span id="reader-0">'.$checked[1].'</span></td>';
				$output .= '</tr>';
			}
		}
	}
	$output .= '</table>';
}
$output .= '</div>';
echo $output;
?>
